package dissertation.extensions.deploy.control;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.compiler.CompileContext;
import com.intellij.openapi.compiler.CompileStatusNotification;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.packaging.artifacts.Artifact;
import com.intellij.packaging.artifacts.ArtifactManager;
import com.intellij.packaging.impl.artifacts.JarArtifactType;
import com.intellij.packaging.impl.elements.ArchivePackagingElement;
import dissertation.common.utils.WriteAction;
import dissertation.services.project.IProjectArtifactService;
import dissertation.services.project.ProjectArtifactService;

import static dissertation.common.constants.Constants.*;

public class EARDeployAction extends AnAction {
    private IProjectArtifactService projectArtifactService;

    public EARDeployAction() {
        projectArtifactService = new ProjectArtifactService();
    }

    public void actionPerformed(AnActionEvent e) {
        final Project project = e.getProject();
        Runnable cmd = new Runnable() {
            @Override
            public void run() {
                if (project != null) {
                    projectArtifactService.removeArtifacts(project);

                    Module[] modules = ModuleManager.getInstance(project).getModules();
                    ArtifactManager artifactManager = ArtifactManager.getInstance(project);
                    for (Module module : modules) {
                        ArchivePackagingElement archivePackagingElement = projectArtifactService.createEARModuleArchivePackagingElement(module);
                        artifactManager.addArtifact(module.getName(), new JarArtifactType(), archivePackagingElement);
                    }

                    ArchivePackagingElement earArchivePackagingElement = projectArtifactService.createEARArchivePackagingElement(project);
                    final Artifact earArtifact = artifactManager.addArtifact(project.getName() + DEPLOYMENT_SUFFIX, new JarArtifactType(), earArchivePackagingElement);

                    projectArtifactService.buildArtifact(new CompileStatusNotification() {
                        @Override
                        public void finished(boolean aborted, int errors, int warnings, CompileContext compileContext) {
                            VirtualFile outputFile = earArtifact.getOutputFile();
                            if (!aborted && errors == 0 && outputFile != null) {
                                Messages.showInfoMessage("Successfully generated EAR archive to " + outputFile.getPath(), "EAR Generation Successful");
                            }
                        }
                    }, project, earArtifact);
                }
            }
        };
        CommandProcessor.getInstance().executeCommand(project, new WriteAction(cmd), null, null);
    }
}
